package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.graph.Graph;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {


    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    @Lazy
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    /**
     * 保存或修改公寓信息的方法
     *
     */
    @Override
    public void saveOrUpdateAll(RoomSubmitVo roomSubmitVo) {
        //判断是修改还是保存，根据Id
        boolean isUpdate=roomSubmitVo.getId()!=null;
        saveOrUpdate(roomSubmitVo);
        Long roomId=roomSubmitVo.getId();
        if(isUpdate){
            //删除中间表信息重新添加
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomId);
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomId);
            roomLabelService.remove(roomLabelLambdaQueryWrapper);

            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomId);
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            LambdaQueryWrapper<RoomLeaseTerm> leaseTermLambdaQueryWrapper=new LambdaQueryWrapper<>();
            leaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomId);
            roomLeaseTermService.remove(leaseTermLambdaQueryWrapper);

            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomId);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }
        if (!CollectionUtils.isEmpty(roomSubmitVo.getAttrValueIds())) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
            for (Long attrValueId : attrValueIds) {
                roomAttrValueList.add(RoomAttrValue.builder().roomId(roomId).attrValueId(attrValueId).build());
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        if (!CollectionUtils.isEmpty(roomSubmitVo.getFacilityInfoIds())) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
            for (Long facilityInfoId : facilityInfoIds) {
                roomFacilityList.add(RoomFacility.builder().roomId(roomId).facilityId(facilityInfoId).build());
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }

        if(!CollectionUtils.isEmpty(roomSubmitVo.getLabelInfoIds())){
            List<RoomLabel> roomLabelList=new ArrayList<>();
            List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
            for (Long labelInfoId : labelInfoIds) {
                roomLabelList.add(RoomLabel.builder().roomId(roomId).labelId(labelInfoId).build());
            }
            roomLabelService.saveBatch(roomLabelList);
        }


        if(!CollectionUtils.isEmpty(roomSubmitVo.getPaymentTypeIds())){
            List<RoomPaymentType> roomPaymentTypeList=new ArrayList<>();
            List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
            for (Long paymentTypeId : paymentTypeIds) {
                roomPaymentTypeList.add(RoomPaymentType.builder().roomId(roomId).paymentTypeId(paymentTypeId).build());
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        if(!CollectionUtils.isEmpty(roomSubmitVo.getLeaseTermIds())){
            List<RoomLeaseTerm> roomLeaseTermList=new ArrayList<>();
            List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
            for (Long leaseTermId : leaseTermIds) {
                roomLeaseTermList.add(RoomLeaseTerm.builder().roomId(roomId).leaseTermId(leaseTermId).build());
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

        if(!CollectionUtils.isEmpty(roomSubmitVo.getGraphVoList())){
            List<GraphInfo> graphInfoList=new ArrayList<>();
            List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
            for (GraphVo graphVo : graphVoList) {
                graphInfoList.add(GraphInfo.builder().url(graphVo.getUrl()).name(graphVo.getName())
                        .itemId(roomId).itemType(ItemType.ROOM).build());
            }
            graphInfoService.saveBatch(graphInfoList);
        }


    }

    @Override
    public IPage<RoomItemVo> customPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.customPage(page,queryVo);
    }

    @Override
    public RoomDetailVo customDetail(Long id) {

        //查询id对应room信息
        RoomInfo roomInfo=getById(id);
        Long roomid=roomInfo.getId();
        //根据room的公寓Id查公寓信息
        ApartmentInfo apartmentInfo=apartmentInfoService.getById(roomInfo.getApartmentId());
        //根据房间id和图片类型查询图片信息
        List<GraphVo> graphVoList=new ArrayList<>();
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomid);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        List<GraphInfo> list = graphInfoService.list(graphInfoLambdaQueryWrapper);
        for (GraphInfo graphInfo : list) {
            graphVoList.add(GraphVo.builder().name(graphInfo.getName()).url(graphInfo.getUrl()).build());
        }
        //根据房间id查询相关的属性信息
        List<AttrValueVo> attrValueVoList=attrValueMapper.selectAttrByRoomId(id);
        //根据房间id查询相关的配套信息
        List<FacilityInfo> facilityInfoList= facilityInfoMapper.selectFacByRoomId(id);
        //根据房间id查询相关的标签信息
        List<LabelInfo> labelInfoList=labelInfoMapper.selectLabelByRoomId(id);
        //根据房间id查询支付方式信息
        List<PaymentType> paymentTypeList=paymentTypeMapper.selectPaymentByRoomId(id);
        //根据房间id查询租期信息
        List<LeaseTerm> leaseTermList=leaseTermMapper.selectLeaseByRoomId(id);
        //新建返回值对象把查询结果装进去
        RoomDetailVo roomDetailVo=new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        return roomDetailVo;
    }

    /**
     * 根据id删除房间信息，要删除房间对应的6个表的信息
     * @param id
     */
    @Override
    public void removeRoom(Long id) {
        //先删除房间自己
        removeById(id);

        //删除中间表信息
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        LambdaQueryWrapper<RoomLeaseTerm> leaseTermLambdaQueryWrapper=new LambdaQueryWrapper<>();
        leaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(leaseTermLambdaQueryWrapper);

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }
}




